/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
ConfigurationUploadWindow = function(configuration) {
	this.configuration = configuration;

 	this.uploadFile = new Ext.ux.form.FileUploadField({fieldLabel: 'Configuration File', emptyText: 'Select a configuration file', width: 464, msgTarget: 'under', scope: this, name: 'upfile'});

    this.form = new Ext.FormPanel({
        labelAlign:'top',
        items: [this.uploadFile],
        border: false,
        fileUpload: true,
        bodyStyle:'background:transparent;padding:10px;'
    });

	ConfigurationUploadWindow.superclass.constructor.call(this, {
        title: 'Upload Configuration File',
        iconCls: 'upload-configuration-icon',
        autoHeight: true,
        width: 500,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: 'hide',

        buttons:[{
            text: 'Upload',
            handler: this.onUpload,
            scope: this
        },{
            text: 'Cancel',
            handler: this.hide.createDelegate(this, [])
        }],

        items: this.form
    });
}

Ext.extend(ConfigurationUploadWindow, Ext.Window, {

    onUpload: function() {
        if(this.uploadFile.getValue() == '')
       	{
       		this.uploadFile.markInvalid("Invalid configuration file!");
       	} else { 
			this.form.getForm().submit({scope: this, url: '/FileUpload?type=1&id='+this.configuration.id, success: this.onSuccessExec, failure: this.onFailureExec, waitMsg: 'Uploading configuration file...'});
       	}
    },
    
    onSuccessExec : function(form, action) {
    	if(action.response.responseText.indexOf("1") != -1) {
			this.el.unmask();
	        this.hide();
	
			this.fireEvent('upload');
    	} else {
    		this.uploadFile.markInvalid("Invalid configuration file!");
    	}
    },    
    
    onFailureExec : function(form, action) {
    	if(action.response.responseText.indexOf("1") != -1) {
			this.el.unmask();
	        this.hide();
	
			this.fireEvent('upload');
    	} else {
    		this.uploadFile.markInvalid("Invalid configuration file!");
    	}
    }
});
